<?php
session_start();//启动会话
//如果未登录，没有设置Session变量“stuNo”,跳转到登录页
if(!isset($_SESSION['stuNo'])){
    header('Location:Login.php?frompage+Students');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生信息</title>
</head>
<body>
    <?php
    include 'HeaderNav.html'; //包含页头与导航页
    ?>
    <h1>学生信息</h1>
    <?php
    //引用数据库连接文件
    require_once 'Conn.php';
    mysqli_query($db,'set names utf8');

    @$allNum = allNews();
       
    @$pageSize = 3; //约定没页显示几条信息
    
    @$pageNum = empty($_GET["pageNum"])?1:$_GET["pageNum"];
    
    @$endPage = ceil($allNum/$pageSize); //总页数
    
    @$array = news($pageNum,$pageSize);
    
    
  
    //mysqli_query($db,'use `itcast`');
    //设置字符集，避免中文乱码
    //$db->query("SET NAMES utf8);
    //显示总页数的函数
     
    function allNews()
     
    {
     
     $coon = mysqli_connect("localhost", "root","");
     
     mysqli_select_db($coon, "xiangmu");
     
     mysqli_set_charset($coon, "utf8");
     
     $rs = "select count(*) num from Student inner join Class on Student.ClassNo=Class.ClassNo"; //可以显示出总页数
     
     $r = mysqli_query($coon, $rs);
     
     $obj = mysqli_fetch_object($r);
     
    // mysqli_close($coon,"zyq");
     
     return $obj->num;
     
    }

    //定义SQL语句，查询所有学生信息
    $sql = "SELECT Student.*,Class.ClassName FROM Student inner join Class on Student.ClassNo=Class.ClassNo";
    // $sql ="SELECT * from Student limit 0,4";
    //执行SQL语句，返回结果，并用表格显示信息
    if($result=  mysqli_query($db,$sql)) {
        echo "<form action='UpdDel.php' method='post'>";
        echo "<input type='submit' name='btnDel' value='删除' onclick='return confirm(\"确定要删除选中的学生信息吗？\");'/>";
        echo " <input type='submit' name'btnUpdate' value='编辑'/>";
        echo "<table class='tb'><tr><th></th><th>学号</th><th>姓名</th><th>班级</th><th>性别</th><th>爱好</th><th>手机</th><th>电子邮箱</th><th>相片</th></tr>";
        // 获取数据
        while ($row = mysqli_fetch_assoc($result)){
            echo "<tr><td><input type='checkbox' name='sel[]' value='".$row["studentID"]."'/>".
            "</td><td>".$row["studentID"].
            "</td><td>".$row["studentName"].
            "</td><td>".$row["ClassName"].
            "</td><td>".$row["sex"].
            "</td><td>".$row["hobby"].
            "</td><td>".$row["mobile"].
            "</td><td>".$row["email"].
            "</td><td><img src='".$row["photo"]."' width='35px' />".
            "</td></tr>";
        }
        echo "</table>";
        echo "</form>";
        //释放结果集
        $result->close();
       //分页的函数
function news($pageNum = 1, $pageSize = 3)
{
  $array = array();
  $coon = mysqli_connect("localhost", "root", "");
  mysqli_select_db($coon, "xiangmu");
  mysqli_set_charset($coon, "utf8");
  // limit为约束显示多少条信息，后面有两个参数，第一个为从第几个开始，第二个为长度
  $rs = "select * from n_content limit " . (($pageNum - 1) * $pageSize) . "," . $pageSize;
  $r = mysqli_query($coon, $rs);
  while ($obj = mysqli_fetch_object($r)) {
    $array[] = $obj;
  }
  mysqli_close($coon,"jereh");
  return $array;
}

    }
    //关闭连接
    $db->close();
    ?>
<div>
  
  <a href="?pageNum=1" rel="external nofollow" rel="external nofollow" >首页</a>
  
  <a href="?pageNum=<?php echo $pageNum==1?1:($pageNum-1)?>" rel="external nofollow" rel="external nofollow" >上一页</a>
  
  <a href="?pageNum=<?php echo $pageNum==$endPage?$endPage:($pageNum+1)?>" rel="external nofollow" rel="external nofollow" >下一页</a>
  
  <a href="?pageNum=<?php echo $endPage?>" rel="external nofollow" rel="external nofollow" >尾页</a>
  
   
  
 </div>
<?php
    include 'Footer.html'; //包含页脚页
    ?>
</body>
</html>